<?php
namespace app\admin\model;
use think\Config;
use think\Model;

class MessageApi extends Model{
     public $username = '';
    public $secret = '';

    public function __construct()
    {
        $this->username = Config::get('site.message_name');
        $this->secret = Config::get('site.message_secret');
    }

    public function send($devid, $message)
    {
        $data = $this->curl("https://open.sw-aiot.com/api/message/sendMsg", [
            'devid' => $devid,
            'times' => date('YmdHis'),
            'reqid' => md5(uniqid() . time()),
            'message' => $message,
        ]);
		 if ($data == false) {
            return -1;
        }
        // dump($data);die;
        if (isset($data['success']) && $data['success'] == true) {
            return true;
        }
        return isset($data['data']["message"])?$data['data']["message"]:$data['message'];
    }

    public function getToken()
    {
        $token =  cache('message_api_token');
        if($token){
            return $token;
        }
        $times = $this->getMillisecond();
        $data = $this->curl("https://open.sw-aiot.com/api/getToken", [
            'username' => $this->username,
            'times' => $times,
            'password' => $this->getPassword($times),
        ], false);
       if (isset($data['data']['token'])) {
        cache('message_api_token', $data['data']['token'],60);
        if ($data['success'] == true) {
            return $data['data']['token'];
			}
        }
       return '';
    }

    public function getPassword($times)
    {
        $data = [
            'username' => $this->username,
            'times' => $times,
            'secret' => $this->secret
        ];
        ksort($data);
        $str = '';
        foreach ($data as $index => $item) {
            $str .= $index . "=" . $item . "&";
        }
        return strtoupper(md5(rtrim($str, "&")));
    }

    public function getMillisecond()
    {
        list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
        return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
    }

    public function curl($url, $data, $isToken = true)
    {
        $strPOST = json_encode($data, JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查   // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        if ($isToken == true) {
            $headers[] = "token:" . $this->getToken();
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }
        curl_close($ch);
        $res = json_decode($result, true);
        return $res;

    }
}

?>